package com.example.black.myapplication.filtercolorrgb;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.black.myapplication.R;

public class ColorRGBFilterView extends View {

    private RectF rectF;
    private Paint paint;
    private ColorMatrix colorMatrix;
    private ColorMatrixColorFilter colorMatrixColorFilter;

    public ColorRGBFilterView(Context context) {
        super(context);
        initConfig();
    }

    public ColorRGBFilterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initConfig();
    }

    public ColorRGBFilterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initConfig();
    }

    private void initConfig() {
        rectF = new RectF(0, 0, 100, 100);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //关闭硬件加速才有效果 ？？？
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);//一定要搞清楚，传入paint和不传入的区别
        //setLayerType(View.LAYER_TYPE_SOFTWARE,paint);//传入paint，颜色过滤器针对两个都生效了
        colorMatrix = new ColorMatrix(new float[]{
                0,0,0,0,0,
                0,0,0,0,0,
                0,0,1,0,0,
                0,0,0,1,0,
        });
        colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.argb(255,100,100,100));
        canvas.drawRect(rectF,paint);
        canvas.translate(200,0);
        paint.setColorFilter(colorMatrixColorFilter);
        canvas.drawRect(rectF,paint);

    }
}
